<? 
session_start();
if(!session_is_registered(uid)){
    echo "You have to login to view this page!</br></br>";
    echo "Click here to return to the "; 
    echo '<a href="./../index.php" title="Login">Login Page</a></br></br>';
    echo "Or sign up using this link ";
    echo '<a href="./../Registration/registration.html" title="Registration">Registration Page</a>';
exit();
}
else

    //Manual SQL Connection
    $con = mysql_connect("localhost", "root", "root") or die ( "Error connectiong to DB: ".mysql_error());
       
    mysql_select_db("reeferslog", $con) or die ( "Error selecting specific DB, reefeerslog: ".mysql_error());

if ($_GET['action'] == 'edit') {
    
    $qry = "SELECT * FROM livestock where user_id='". $_SESSION['uid'] ."' and lv_id='".$_GET['lv_id']."'";
    
    $results = mysql_query($qry)or die(mysql_error($con));

    extract(mysql_fetch_assoc($results));
    
}else {
    //set values blanks
    $category = '';
    $name = '';
    $type = '';
    $supplier = '';
    $cost = 0;
    
    //Date variables
    $day = 0;
    $month = 0;
    $year = 0;
   
    
    //Function to make a valid date input
    $purchase_date = $year."-".$month."-".$day;
    
    list($year, $month, $day) = explode("-", $purchase_date);
}

?>

<html>
<head>
    <title><?php echo ucfirst($_GET['action']); ?>Stock</title>
    <link rel="stylesheet" type="text/css" href="http://localhost/ReefersLogApp/CSS/Generalpagelayout.css" />
    
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>
    <script type='text/javascript' src='./../scripts/js/jquery.ba-hashchange.min.js'></script>
    <script type='text/javascript' src='./../scripts/js/dynamicpage.js'></script>
    
</head>

<div id="container">
	<div id="header">
            <h1>
		Reefers Log
            </h1>
	</div>
	<div id="navigation">
		<ul>
			<li><a href="./../WelcomePage/WelcomePage.php">Home</a></li>
			<li><a href="./../Profile/userprofile.php">Profile</a></li>
			<li><a href="./../Tools/tools.php">Tools</a></li>
			<li><a href="./../Logout/logout.php">Logout</a></li>
		</ul>
	</div>
	<div id="content-container">
		<div id="section-navigation">
                    <object id="loader" name="frameloader" type="text/html" data="http://localhost/ReefersLogApp/Welcome/WelcomePage.php/"></object>
			<ul>
                           
                                <li><a href="fullsotcklist.php">Livestock List</a></li>
				<li><a href="fish.php">Fish List</a></li>
				<li><a href="coral.php">Coral List</a></li>	
                                <li><a href="invert.php">Invert List</a></li>
                                
				
			</ul>
		</div>
		<div id="content">
                    <h2>
                        Your Aquarium Statistics:
                    </h2>
                    <p>
                        Edit your livestock logging regarding your aquarium. You may complete the form below or select another option
                        from the menu on the left to track your equipment.
                    </p>
		</div>
            <div id="content">
                    <h2>
                        Update the form below:
                    </h2>
                

<div align="center">

    <form action="commitedit.php?action=<?php echo $_GET['action']?>&type=invert&lv_id=<?php echo $_GET['lv_id'];?>" method="post">

        <table align="center" border="1">
                            <thead>
                                <th>
                                    Livestock List:
                                </th>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>Category:<select name="category">
                                            <option value="" SELECTED></option>
                                            <?php 
                                            $cat = mysql_query("select lv_id, category from livestock where user_id='".$_SESSION['uid']."' and lv_id='".$_GET['lv_id']."' order by category");
                                            while($row = mysql_fetch_array($cat)){
                                               $selected = " SELECTED";
                                               if ( $row['category'] == $category){
                                                           $selected = " SELECTED";
                                                        } else {
                                                           $selected = "";
                                                        }
                                                        echo '<OPTION value="'.$row['lv_id'].'"'.$selected.'>'.$row['category'].'</OPTION>'."\r\n";
                                                     }
                                            ?>
                                            <option>Fish</option>
                                            <option>Coral</option>
                                            <option>Invert</option>
                                        </select> </td>                                    
                                </tr>
                                <tr>
                                    <td>Name:<input type="text" name="name" value="<?echo "$name"?>"</td>
                                </tr>
                                <tr>
                                    <td>Type:<input type="text" name="type" value="<?echo "$type"?>"</td>
                                </tr>
                                <tr>
                                    <td>Cost:<input type="text" name="cost" value="<?echo "$cost"?>"</td>
                                </tr>
                                <tr>
                                    <td>Supplier:<input type="text" name="supplier" value="<?echo "$supplier"?>"</td>
                                </tr>
                                
                                <tr>
                                    <td>Purchase Date (MM/DD/YYYY):</br><select name="month">
                                            <option value="" SELECTED>
                                            <?php 
                                            $month = mysql_query("select lv_id, purchase_date from livestock where user_id='".$_SESSION['uid']."' and lv_id='".$_GET['lv_id']."' order by purchase_date desc");
                                            while($row = mysql_fetch_array($month)){
                                               $selected = " SELECTED";
                                               if ( $row['purchase_date'] == $purchase_date){
                                                           $selected = " SELECTED";
                                                           list($year, $month, $day) = explode("-", $purchase_date);
                                                        } else {
                                                           $selected = "";
                                                        }
                                                        echo '<OPTION value="'.$row['lv_id'].'"'.$selected.'>'.$month.'</OPTION>'."\r\n";                                                                                                                                                                                 
                                            }
                                            ?>
                                        <option>01</option>
                                        <option>02</option>
                                        <option>03</option>
                                        <option>04</option>
                                        <option>05</option>
                                        <option>06</option>
                                        <option>07</option>
                                        <option>08</option>
                                        <option>09</option>
                                        <option>10</option>
                                        <option>11</option>
                                        <option>12</option>
                                    </select><select name="day">
                                        <option value="" SELECTED>
                                            <?php 
                                            $d = mysql_query("select lv_id, purchase_date from livestock where user_id='".$_SESSION['uid']."' and lv_id='".$_GET['lv_id']."' order by purchase_date desc");
                                            while($row = mysql_fetch_array($d)){
                                               $selected = " SELECTED";
                                               if ( $row['purchase_date'] == $purchase_date){
                                                           $selected = " SELECTED";
                                                           list($year, $month, $day) = explode("-", $purchase_date);
                                                        } else {
                                                           $selected = "";
                                                        }
                                                        echo '<OPTION value="'.$row['lv_id'].'"'.$selected.'>'.$day.'</OPTION>'."\r\n";                                                                                                                                                                                 
                                            }
                                            ?>
                                        <option>01</option>
                                        <option>02</option>
                                        <option>03</option>
                                        <option>04</option>
                                        <option>05</option>
                                        <option>06</option>
                                        <option>07</option>
                                        <option>08</option>
                                        <option>09</option>
                                        <option>10</option>
                                        <option>11</option>
                                        <option>12</option>
                                        <option>13</option>
                                        <option>14</option>
                                        <option>15</option>
                                        <option>16</option>
                                        <option>17</option>
                                        <option>18</option>
                                        <option>19</option>
                                        <option>20</option>
                                        <option>21</option>
                                        <option>22</option>
                                        <option>23</option>
                                        <option>24</option>
                                        <option>25</option>
                                        <option>26</option>
                                        <option>27</option>
                                        <option>28</option>
                                        <option>29</option>
                                        <option>30</option>
                                        <option>31</option>
                                    </select><select name="year">
                                        <option value="" SELECTED>
                                            <?php 
                                            $y = mysql_query("select lv_id, purchase_date from livestock where user_id='".$_SESSION['uid']."' and lv_id='".$_GET['lv_id']."' order by purchase_date desc");
                                            while($row = mysql_fetch_array($y)){
                                               $selected = " SELECTED";
                                               if ( $row['purchase_date'] == $purchase_date){
                                                           $selected = " SELECTED";
                                                           list($year, $month, $day) = explode("-", $purchase_date);
                                                        } else {
                                                           $selected = "";
                                                        }
                                                        echo '<OPTION value="'.$row['lv_id'].'"'.$selected.'>'.$year.'</OPTION>'."\r\n";                                                                                                                                                                                 
                                            }
                                            ?>
                                        <option>2012</option>
                                        <option>2013</option>
                                        <option>2014</option>
                                        <option>2015</option>
                                        <option>2016</option>
                                        <option>2017</option>
                                        <option>2018</option>
                                        <option>2019</option>
                                        <option>2020</option>
                                        <option>2021</option>
                                        <option>2022</option>
                                        <option>2023</option>
                                        <option>2024</option>
                                        <option>2025</option>
                                        <option>2026</option>
                                        <option>2027</option>
                                        <option>2028</option>
                                        <option>2029</option>
                                        <option>2030</option>                                      
                                    </select></td>
                                </tr>                                                      
                    
</tbody>
</table>
    <input type="submit" value="Edit Stock" name="EditStock" />
</div>
</form>
</div>
		<div id="aside">
                    <h3>
                        Info:
                    </h3>
                    <p>Edit your livestock data by completing the form.</p>
		</div>
		<div id="footer">
			Copyright © ReefersLog, 2012, wkoester All Rights Reserved
		</div>
	</div>
</div>
</html>